import { defineStore } from 'pinia'
import type { CatalogType, MemoType } from './type'
import { geneId, localGetItem, localSetItem } from '@/utils'
const indexStore = defineStore('index', {
    state: () => ({
        catalogs: [] as CatalogType[],
        memos: [] as MemoType[],
        active_cataid: null as number | null,
        active_memoid: null as number | null
    }),
    actions: {
        //获取目录列表
        getCatalogs() {
            let data = localGetItem("catalogs");
            if (data) {
                this.catalogs = data;
            }
        },
        //获取备忘录列表
        getMemos() {
            let data = localGetItem("memos");
            if (data) {
                this.memos = data;
            }
        },
        //设置备忘录ID
        setMemoId(id: number | null) {
            this.active_memoid = id;
            localSetItem("active_memoid", id);
        },
        setCataId(id: number | null) {
            this.active_cataid = id;
            localSetItem("active_cataid", id);
        },
        //新建文件夹
        createCata(val: CatalogType) {
            let curcata = Object.assign({}, val, {
                // cata_id:geneId()
            });
            this.catalogs.push(curcata);
            localSetItem("catalogs", this.catalogs);
        },
        // 创建备忘录
        createMemo(val: Pick<MemoType, "title" | "cata_id" | "content">) {
            let memo = Object.assign({}, val, {
                memo_id: geneId(),
                update_at: new Date().valueOf(),
            });
            this.memos.push(memo);
            localSetItem("memos", this.memos);
        },
        // 修改备忘录
        updateMemo(id: number, data: Partial<MemoType>) {
            let index = this.memos.findIndex((r) => r.memo_id == id);
            if (index >= 0) {
                this.memos[index] = { ...this.memos[index], ...data };
                localSetItem("memos", this.memos);
            }
        },
        // 删除备忘录
        removeMemo(id: number) {
            let index = this.memos.findIndex((r) => r.memo_id == id);
            if (index >= 0) {
                this.memos.splice(index, 1);
                localSetItem("memos", this.memos);
            }
        },
        getActiveId() {
            this.active_cataid = localGetItem("active_cataid");
            this.active_memoid = localGetItem("active_memoid");
        },
    },
    getters: {
        activeMemos: (state) => {
            return state.memos.filter((r) => r.cata_id == state.active_cataid)
        }

    },


})

export default indexStore